<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
</head>
<!-- 引入 layui.css -->
<link rel="stylesheet" href="//unpkg.com/layui@2.6.8/dist/css/layui.css">
<!-- 引入 layui.js -->
<script src="//unpkg.com/layui@2.6.8/dist/layui.js"></script>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script src="./js/fun.js"></script>

<body>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">学号或工号</label>
        <div class="layui-input-block">
            <input type="text" name="id" required  lay-verify="required" placeholder="请输入学号或者工号" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-inline">
            <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>

    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">验证码</label>
        <div class="layui-input-inline">
            <input type="password" name="captcha" required lay-verify="required" placeholder="请输入验证码" autocomplete="off" class="layui-input">
            <img src=""  class="Captcha" style="height: 50px" alt="验证码">
        </div>

    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">请选择您的身份</label>
        <div class="layui-input-block">
            <select name="permission" lay-verify="required">
                <option value="0">学生</option>
                <option value="1">教师或管理员</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">登录</button>
            <a href="sturegist.html">去注册</a>
        </div>
    </div>
</form>
</body>
<script>
    function changeCaptcha(){


    }

    $(".Captcha").click(function (){
        // alert(1)
        $(".Captcha").attr("src",Api+"/captcha/getImgCaptcha?"+new Date().getDate())
    })

    layui.use(['form','layer'], function(){
        var form = layui.form;
        var layer = layui.layer;

        //监听提交
        form.on('submit(formDemo)', function(data){
            // layer.msg(JSON.stringify(data.field));
            let loginData = data.field
            let apiurl = "/stu/login"
            if(loginData.permission == "1"){
                apiurl = "/Teacher/login"
            }
            $.post(Api+apiurl,
                {
                    id:loginData.id,
                    password:loginData.password
                },
            function (res){
                if(res.code == "200"){
                    layer.msg("登录成功，欢迎你,"+res.data.name)
                    setCookie("premission",res.data.pre==null?loginData.permission:res.data.pre)
                    setCookie("userid",res.data.id)
                    setCookie("major",res.data.majorid)
                    setCookie("user",JSON.stringify(res.data))

                    if(loginData.permission == "1"){
                        window.location.href = "admin/index.html?pre="+loginData.permission
                    }else {
                        window.location.href = "index.html?pre="+loginData.permission
                    }

                }else {
                    layer.msg("登录失败，账号或密码错误")
                }
            })
            return false;
        });
    });
</script>
</html>